; YALC client
;
; Copyright (c) 2011, Solution
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;     * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;     * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;     * Neither the name of the Solution nor the
;       names of its contributors may be used to endorse or promote products
;       derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL SOLUTION BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Global Version$ = "rev 6"
Global Server.s
Global Port.s
Global DebugEnabled
Global LogFile.s
Global Lang.s

Enumeration
  #hfont
  #window
  #config
  #log
  #tabs
  #roster
  #input
  #statusbar
  #serverinput
  #serverinputstring
  #portinput
  #portinputstring
  #langinput
  #langinputstring
  #savebutton
  #langdir
  #conoplogo
  #conoptext
EndEnumeration

; procedures
IncludeFile("func_global.pb")
IncludeFile("func_proto.pb")
IncludeFile("func_client.pb")
UsePNGImageDecoder()

; detecting where we are...
Global RunDir.s = GetPathPart(ProgramFilename())
CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
  Global ConfDir.s = "/opt/yalc/"
CompilerEndIf

; loading fonts and images
LoadFont(#hfont,"Tahoma",14)
ConnectionOptionsLogo = CatchImage(0,?ConnectionOptionsLogo)
CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
  DataSection
    ConnectionOptionsLogo: IncludeBinary "imageset/connection_options.png"
  EndDataSection
CompilerElse
  DataSection
    ConnectionOptionsLogo: IncludeBinary "imageset\connection_options.png"
  EndDataSection
CompilerEndIf

; loading settings
CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
  If client_LoadSettings(ConfDir + "yalc.ini")
    HaveSettings = #True
  Else
    HaveSettings = #False
  EndIf
CompilerElse
  If client_LoadSettings(RunDir + "yalc.ini")
    HaveSettings = #True
  Else
    HaveSettings = #False
  EndIf
CompilerEndIf

If HaveSettings And LogFile
  CreateFile(#log,LogFile)
  global_AddToLog(FormatDate("%hh:%ii:%ss",Date()) + ": YALC " + Version$ + " started")
EndIf

; creating window
If OpenWindow(#window,#PB_Ignore,#PB_Ignore,780,560,"YALC " + Version$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If Not HaveSettings
    ResizeWindow(#window,#PB_Ignore,#PB_Ignore,300,180)
    ImageGadget(#conoplogo,0,0,48,48,ConnectionOptionsLogo)
    TextGadget(#conoptext,49,10,250,38,lang_conoptions$,#PB_Text_Center)
    SetGadgetFont(#conoptext,FontID(#hfont))
    TextGadget(#langinputstring,10,53,180,20,lang_inputlang$,#PB_Text_Right)
    ComboBoxGadget(#langinput,200,50,90,20)
    AddGadgetItem(#langinput,-1,lang_info$)
    SetGadgetState(#langinput,0)
    If client_GetLangList()
      ForEach LangList()
        AddGadgetItem(#langinput,-1,LangList())
      Next
    Else
      DisableGadget(#langinput,1)
    EndIf
    TextGadget(#serverinputstring,10,83,180,20,lang_inputserver$,#PB_Text_Right)
    IPAddressGadget(#serverinput,200,80,90,20)
    TextGadget(#portinputstring,10,113,180,20,lang_inputport$,#PB_Text_Right)
    StringGadget(#portinput,200,110,90,20,"")
    ButtonGadget(#savebutton,200,140,90,30,lang_okbutton$)
    Repeat
      Select WaitWindowEvent(100)
          
        ; gadget events
        Case #PB_Event_Gadget
          If EventGadget() = #savebutton And EventType() = #PB_EventType_LeftClick
            If client_IsIPAndPort(GetGadgetText(#serverinput),Val(GetGadgetText(#portinput)))
              Server.s = GetGadgetText(#serverinput)
              Port.s = GetGadgetText(#portinput)
              CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
                If client_SaveSettings(ConfDir + "yalc.ini")
                  If IsFile(#log)
                    CloseFile(#log)
                  EndIf
                  RunProgram(ProgramFilename())
                  global_Exit(0)
                Else
                  MessageRequester("Error!",lang_cantsave$)
                  global_Exit(0)
                EndIf
              CompilerElse
                If client_SaveSettings(RunDir + "yalc.ini")
                  If IsFile(#log)
                    CloseFile(#log)
                  EndIf
                  RunProgram(ProgramFilename())
                  global_Exit(0)
                Else
                  MessageRequester("Error!",lang_cantsave$)
                  global_Exit(0)
                EndIf
              CompilerEndIf
            Else
              MessageRequester("Error!",lang_wrongipportformat$)
            EndIf
          EndIf
          
          If EventGadget() = #langinput And EventType() = #PB_EventType_Change
            Lang = StringField(ReplaceString(GetGadgetText(#langinput),"]",""),2,"[")
            client_LoadLang(Lang)
            SetGadgetText(#langinputstring,lang_inputlang$)
            SetGadgetText(#serverinputstring,lang_inputserver$)
            SetGadgetText(#portinputstring,lang_inputport$)
            SetGadgetText(#conoptext,lang_conoptions$)
            SetGadgetText(#savebutton,lang_okbutton$)
          EndIf
          
        ; if close button pressed or alt-f4 catched
        Case #PB_Event_CloseWindow
          global_Exit(0)
          
      EndSelect
    ForEver
  EndIf
  If Not InitNetwork()
    If LogFile
      global_AddToLog(FormatDate("%hh:%ii:%ss",Date()) + ": Can't init network! Exiting now...")
    EndIf
    MessageRequester("Error!",lang_cantinitnetwork$)
    global_Exit(0)
  EndIf
  CreateStatusBar(#statusbar,WindowID(#window))
  AddStatusBarField(100)
  StatusBarText(#statusbar,0,"status")
  PanelGadget(#tabs,0,40,540,500)
  AddGadgetItem(#tabs,-1,"tab 1")
  AddGadgetItem(#tabs,-1,"tab 2")
  AddGadgetItem(#tabs,-1,"tab 3")
  CloseGadgetList()
  TreeGadget(#roster,540,40,280,499,#PB_Tree_NoLines)
  AddGadgetItem(#roster,-1,"Group 1",0,0)
  AddGadgetItem(#roster,-1,"User 1",0,1)
  AddGadgetItem(#roster,-1,"User 2",0,1)
  AddGadgetItem(#roster,-1,"User 3",0,1)
  AddGadgetItem(#roster,-1,"Group 2",0,0)
  AddGadgetItem(#roster,-1,"User 1",0,1)
  AddGadgetItem(#roster,-1,"User 2",0,1)
  AddGadgetItem(#roster,-1,"User 3",0,1)
  
Else
  If LogFile
    global_AddToLog(FormatDate("%hh:%ii:%ss",Date()) + ": Can't create window! Exiting now...")
  EndIf
  global_Exit(1)
EndIf

; main thread
Repeat
  ev = WaitWindowEvent(100)
  If ev = #PB_Event_CloseWindow
    global_Exit(0)
  EndIf
ForEver
; IDE Options = PureBasic 4.60 (Windows - x86)
; CursorPosition = 27
; EnableXP